package com.xiaoxi.proxymode;

/**
 * @Author xiaoxi
 * @ProjectName design_mode
 * @PackageName com.xiaoxi.proxymode
 * @ClassName ProxyPrinter
 * @Description TODO
 * @Date 2023/4/6 21:52
 * @Version 1.0
 */
public class ProxyPrinter implements Printable {
    private String name;
    private Printer printer;

    public ProxyPrinter() {
    }

    public ProxyPrinter(String name) {
        this.name = name;
    }

    @Override
    public synchronized void setPrinterName(String name) {
        this.name = name;
        if (printer != null) {
            printer.setPrinterName(name);
        }
    }

    @Override
    public String getPrinterName() {
        return name;
    }

    @Override
    public void print(String str) {
        realize();
        printer.print(str);
    }

    private synchronized void realize() {
        if (printer == null) {
            printer = new Printer(name);
        }
    }


}
